#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void func(){
    //char retval[] = "nihao~!!";
    char *retval = "nihao~!!";
    pthread_exit(retval); //retval是线程的退出返回值
}
void *entry(void *arg)
{
    printf("3-----%p\n", arg);
   //  sleep(5);
   //  func();
    while(1) {
        printf("i am child thread! %s\n", (char*)arg);
        sleep(1);
    }
    return NULL;
}
int main()
{
    pthread_t tid;
    char *buf = "hard life!";
    printf("1-----%p\n", "hard life!");
    printf("2-----%p\n", buf);
    //pthread_create(线程id空间, 属性，入口函数，传入参数)
    //成功返回0，失败返回非0
    int ret = pthread_create(&tid, NULL, entry, (char*)buf);
    if (ret != 0) {
        printf("thread  create error!\n");
        return 0;
    }

    /*
    pthread_detach(tid);
    void *retval;
    pthread_join(tid, &retval);
    printf("tid:%p------%s\n", tid, retval);*/
    sleep(3);
    pthread_cancel(tid);


    void *retval;
    pthread_join(tid, &retval);
    printf("tid:%p------%s\n", tid, retval);
    while(1) {
        printf("i am main thread!\n");
        sleep(1);
    }
    return 0;
}
